<?php
include_once '../functions.php';

$data_dir = '../data/';
$blog_db = $data_dir . 'posts.ini';
$drafts_db = $data_dir . 'drafts.ini';
$blog_users = $data_dir . 'users.ini.php';
$blog_config = $data_dir . 'config.ini';
$configurations = parse_ini_file($blog_config);

function asplode($string) {
	$string = stripslashes($string);
	$string = preg_replace( "/=/", '&equals;', $string);
	$string = preg_replace( "/\'/", '&apos;', $string);
	$string = preg_replace( "/\"/", '&quot;', $string);
	return $string;
}

function rel_date($posted_date) {
	// thanks to maniacal rage, i converted this script for direct timestamp access
	// http://graveyard.maniacalrage.net/archives/2004/02/relativedatesusing/
	$relative_date = "";
	$in_seconds = $posted_date;
	
    $diff = time()-$in_seconds;
    $months = round($diff/2592000);
    $weeks = round($diff/604800);
    $days = round($diff/86400);
    $hours = round($diff/3600);
    $minutes = round($diff/60);
    $seconds = $diff;

    if ($months>0) {
	    $relative_date .= ($relative_date?', ':'').$months.' month'.($months>1?'s':'');        
	} elseif ($days>0) {
	    // days and hours
	    $relative_date .= ($relative_date?', ':'').$days.' day'.($days>1?'s':'');
	} elseif ($hours>0) {
	    // hours and minutes
	    $relative_date .= ($relative_date?', ':'').$hours.' hour'.($hours>1?'s':'');
	    $relative_date .= $minutes>0?($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':''):'';
	} elseif ($minutes>0) {
	    // minutes only
	    $relative_date .= ($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':'');
	} else {
	    // seconds only
	    $relative_date .= ($relative_date?', ':'').$seconds.' second'.($seconds>1?'s':'');
    }
    // show relative date and add proper verbiage
    return $relative_date.' ago';
}

function array_write($array_source, $file) {
	foreach($array_source as $key => $value) {
		$data = "";
		$data .= "[" . $key . "]\n";
		if ($value == is_array($value)) {
			foreach ($value as $subkey => $subvalue) {
				$data .= $subkey;
				$data .= '="';
				$data .= $subvalue;
				$data .= "\"\n";
			}
		}
	}
	$fp = fopen($file, "w");
	fwrite ($fp, $data);
	fclose ($fp);
}

function preview($id, $db) {
	show_one($id);
}

function list_posts($db) {
	$array = parse_ini_file($db, true);
	$i = 0;
	foreach($array as $key => $value) {
		echo '<tr>' . "\n\t";
		
		echo '<td>';
		echo '<input style="width:1em;" name="boxes" value="'. $i++ . '" id="' . $key . '" type="checkbox" />';
		echo '</td>' . "\n\t";
				
		echo '<td style="text-align:left;">';
		echo '<a href="page.php?p=';
		if ($db == "drafts.ini") {
			echo 'drafts';
		}
		else {
			echo 'manage';
		}
		echo '&view=' . $key . '">' . $value['title'] . '</a>';
		echo '</td>' . "\n\t";
								
		echo '<td>';
		echo rel_date($key);
		echo '</td>' . "\n";
		echo '</tr>' . "\n\n";
	}
}
	
function add_post($title, $content, $id, $postdate, $moddate, $author, $db) {
	$original = file_get_contents($db);
	// get rid of those pesky equal signs, and apostrophes
	$content = asplode($content);
	$title = asplode($title);
	
	// make the category the timestamp
	$data = "[$id]\n";
	// our title
	$data .= "title = \"$title\"\n";
	// and our content
	$data .= "content = \"$content\"\n";
	// and our author
	$data .= "author = \"" . $author . "\"\n";
	// and our creation date (just in case...something bad happens to our ID)
	$data .= 'creationdate = "' . $id . '"' . "\n";
	// post date (should be different, but can be blank)
	$data .= 'postdate = "' . $postdate . '"' . "\n";
	// modified date (should be revised on every change)
	$data .= 'moddate = "' . $moddate . '"' . "\n";

	$data .="\n";
	
	$data .= $original;
	// open our file
	$fp = fopen($db, "w");
	// write to the stream
	fwrite ($fp, $data);
	// close it up
	fclose ($fp);	
	echo $data;
}

function delete_post($id, $db) {
	$content = parse_ini_file($db, true);
	if (array_key_exists($id, $content)){
		$old_title = $content[$id]['title'];
		$result = 'The post <strong>"' . $old_title . '"</strong>was deleted succesfully.';		
		unset($content[$id]);
		array_write($content, $db);
	}
	else { $result = 'The entry' . $id . ' does not exist.';}
	return ($result);
}

?>